﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WHC.Hotel.BLL;
using WHC.Hotel.Entity;
using WHC.OrderWater.Commons;


namespace WHC.Hotel.UI
{
    public partial class FrmBookTeaSuite : BaseForm
    {
        public TeaSuiteInfo RoomInfo = null;
        private List<string> AddedRooms = new List<string>();
        public TeaBookInfo BookInfo = null;

        public FrmBookTeaSuite()
        {
            InitializeComponent();
            InitDictItems();
        }

        private void InitDictItems()
        {
            this.txtRoomType.Items.Clear();
            List<TeaSuiteTypeInfo> typeList = BLLFactory<TeaSuiteType>.Instance.GetAll();
            foreach (TeaSuiteTypeInfo typeInfo in typeList)
            {
                this.txtRoomType.Items.Add(new CListItem(typeInfo.Name));
            }
        }

        private void FrmBookRoom_Load(object sender, EventArgs e)
        {
            if (RoomInfo != null)
            {
                this.txtRoomType.Text = RoomInfo.RoomType;
                this.txtRoomNo.Text = RoomInfo.RoomNo;
                this.AddedRooms.Add(RoomInfo.RoomNo);
                RefreshAdded();
            }
            else if (BookInfo != null)
            {
                this.txtRoomNo.Text = BookInfo.RoomNo;
                TeaSuiteInfo info = BLLFactory<TeaSuite>.Instance.FindByRoomNo(BookInfo.RoomNo);
                this.txtRoomType.Text = info.RoomType;
                this.txtRoomNo.Text = info.RoomNo;
                this.txtBookNo.Text = BookInfo.BookNo;
                this.txtName.Text = BookInfo.CustomerName;
                this.txtNote.Text = BookInfo.Note;
                this.txtPreArrive.Value = BookInfo.PreArrivalTime;
                this.txtPreOff.Value = BookInfo.PreOffTime;
                this.txtRetention.Value = BookInfo.RetentionTime;
                this.txtTelephone.Text = BookInfo.CustomerTel;
                this.btnAdd.Enabled = false;

                this.AddedRooms.Add(BookInfo.RoomNo);
                RefreshAdded();
            }
        }

        private void txtRoomType_SelectedIndexChanged(object sender, EventArgs e)
        {
            CListItem item = this.txtRoomType.SelectedItem as CListItem;
            if (item != null)
            {
                this.txtRoomNo.Items.Clear();
                List<TeaSuiteInfo> roomList = BLLFactory<TeaSuite>.Instance.FindRooms(item.Value, "");
                foreach (TeaSuiteInfo info in roomList)
                {
                    this.txtRoomNo.Items.Add(info.RoomNo);
                }
            }
        }

        private void txtRoomNo_SelectedIndexChanged(object sender, EventArgs e)
        {
            string roomNo = this.txtRoomNo.Text;
            ShowBooked(roomNo);
        }

        private void ShowBooked(string roomNo)
        {
            this.lblRoomNo.Text = string.Format("餐台号:{0}", roomNo);
            List<TeaBookInfo> bookList = BLLFactory<TeaBook>.Instance.FindByRoomNo(roomNo);
            this.lvwBookGuest.Items.Clear();
            foreach (TeaBookInfo info in bookList)
            {
                ListViewItem item = new ListViewItem(info.CustomerName);
                item.SubItems.Add(info.PreArrivalTime.ToString());
                item.SubItems.Add(info.CustomerTel);
                item.Tag = info;
                this.lvwBookGuest.Items.Add(item);
            }
        }

        private void RefreshAdded()
        {
            this.lvwBookRooms.Items.Clear();
            foreach(string roomNo in this.AddedRooms)
            {
                TeaSuiteInfo info = BLLFactory<TeaSuite>.Instance.FindByRoomNo(roomNo);
                if (info != null)
                {
                    ListViewItem item = new ListViewItem(info.RoomType);
                    item.SubItems.Add(roomNo);
                    this.lvwBookRooms.Items.Add(item);
                }
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (this.txtRoomType.Text.Length > 0 && this.txtRoomNo.Text.Length > 0)
            {
                if (!AddedRooms.Contains(this.txtRoomNo.Text))
                {
                    this.AddedRooms.Add(this.txtRoomNo.Text);
                }
                RefreshAdded();
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.None;
            if (this.txtName.Text.Length == 0)
            {
                MessageUtil.ShowTips("宾客名称不能为空");
                this.txtName.Focus();
                return;
            }

            try
            {
                if (BookInfo != null)//只是需要更新客户信息的一个订单信息，房间信息不更新
                {
                    BookInfo.BookNo = this.txtBookNo.Text;
                    BookInfo.CustomerName = this.txtName.Text;
                    BookInfo.CustomerTel = this.txtTelephone.Text;
                    //BookInfo.IsCancel = false;
                    BookInfo.Note = this.txtNote.Text;
                    BookInfo.PreArrivalTime = this.txtPreArrive.Value;
                    BookInfo.PreOffTime = this.txtPreOff.Value;
                    BookInfo.RetentionTime = this.txtRetention.Value;

                    BLLFactory<TeaBook>.Instance.Update(BookInfo, BookInfo.ID.ToString());
                    this.DialogResult = DialogResult.OK;
                }
                else
                {
                    foreach (string roomNo in AddedRooms)
                    {
                        TeaBookInfo info = new TeaBookInfo();
                        info.BookNo = this.txtBookNo.Text;
                        info.CustomerName = this.txtName.Text;
                        info.CustomerTel = this.txtTelephone.Text;
                        info.Note = this.txtNote.Text;
                        info.PreArrivalTime = this.txtPreArrive.Value;
                        info.PreOffTime = this.txtPreOff.Value;
                        info.RetentionTime = this.txtRetention.Value;
                        info.RoomNo = roomNo;

                        BLLFactory<TeaBook>.Instance.Insert(info);
                        BLLFactory<TeaSuite>.Instance.SetRoomStatus(roomNo, TeaSuiteStatus.预定.ToString());
                        this.DialogResult = DialogResult.OK;
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                MessageUtil.ShowError(ex.Message);
            }
        }

        private void lvwBookRooms_DoubleClick(object sender, EventArgs e)
        {
            if (this.lvwBookRooms.SelectedItems.Count == 0)
            {
                return;
            }

            ListViewItem item = this.lvwBookRooms.SelectedItems[0];
            if (item != null)
            {
                this.AddedRooms.Remove(item.Text);
            }
            RefreshAdded();
        }

        private void menu_Cancel_Click(object sender, EventArgs e)
        {
            if (this.lvwBookGuest.SelectedItems.Count > 0)
            {
                ListViewItem item = this.lvwBookGuest.SelectedItems[0];
                TeaBookInfo bookInfo = item.Tag as TeaBookInfo;
                if (bookInfo != null)
                {
                    BLLFactory<TeaBook>.Instance.UpdateBookIn(bookInfo.ID, true);
                    ShowBooked(this.txtRoomNo.Text);
                }
            }            
        }

        private void menu_Delete_Click(object sender, EventArgs e)
        {
            if (this.lvwBookGuest.SelectedItems.Count > 0)
            {
                ListViewItem item = this.lvwBookGuest.SelectedItems[0];
                TeaBookInfo bookInfo = item.Tag as TeaBookInfo;
                if (bookInfo != null)
                {
                    BLLFactory<TeaBook>.Instance.Delete(bookInfo.ID.ToString());
                    ShowBooked(this.txtRoomNo.Text);
                }
            }            
        }
    }
}
